package com.quoo.struts2.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
//import org.hibernate.cfg.Configuration;
//import org.hibernate.service.ServiceRegistry;
//import org.hibernate.service.ServiceRegistryBuilder;

import com.quoo.struts2.dao.UserDAO;
import com.quoo.struts2.domain.User;

// is used in hibernate 4 
public class UserDAOImpl implements UserDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void saveUser(User user) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
		session.close();
	}

	public void removeUser(User user) {
		// this.getHibernateSession().beginTransaction();
		// this.getHibernateSession().delete(user);
		// this.getHibernateSession().getTransaction().commit();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(user);
		session.getTransaction().commit();
		session.close();
	}

	public User findUserById(Integer id) {
		// this.getHibernateSession().beginTransaction();
		// User user = (User) this.getHibernateTemplate().get(User.class, id);
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		User user = (User) session.get(User.class, id);
		// this.getHibernateSession().getTransaction().commit();
		session.getTransaction().commit();
		session.close();
		return user;
	}

	@SuppressWarnings("unchecked")
	public List<User> findAllUsers() {
		String hql = "from User user";
		// this.getHibernateSession().beginTransaction();
		// return (List<User>) this.getHibernateTemplate().find(hql);
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		List<User> user = (List<User>) session.createQuery(hql).list();
		session.getTransaction().commit();
		session.close();
		return user;
	}

	public void updateUser(User user) {

		// this.getHibernateSession().beginTransaction();
		// this.getHibernateSession().update(user);
		// this.getHibernateSession().getTransaction().commit();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(user);
		session.getTransaction().commit();
		session.close();
	}

	public Boolean validate(String username, String password) {
		Boolean isValidUser = Boolean.FALSE;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session
				.createSQLQuery("select * from Login login where login.username=:username and login.password=:password");
		query.setParameter("username", username);
		query.setParameter("password", password);
		if (query.list().size() > 0) {
			isValidUser = Boolean.TRUE;
		}
		session.getTransaction().commit();
		session.close();
		return isValidUser;
	}

}
